// src/components/SEO.js
import React from 'react'
import { graphql, useStaticQuery } from 'gatsby'
import { Helmet } from 'react-helmet'

export default function SEO ({title, description, meta, lang}) {
    const {site} = useStaticQuery(graphql`
        query {
            site {
                siteMetadata {
                    title
                    description
                }
            }
        }
    `)
    const metaDescription = description || site.siteMetadata.description
    return <Helmet 
        htmlAttributes={{ lang }}
        title={title}
        //  %s 占位符 会被替换成 SEO 组件传递的 title 属性
        titleTemplate={`%s | ${site.siteMetadata.title}`}
        meta={[
            {
                name: 'description',
                content: metaDescription
            }
        ].concat(meta)}
    />
}

// 设置默认值
SEO.defaultProps = {
    description: "test description",
    meta: [],
    lang: "en"
}